//
//  HLTabBarViewController.swift
//  HLXiMaLaYa
//
//  Created by hulinhu on 2018/1/9.
//  Copyright © 2018年 hulinhu. All rights reserved.
//

import UIKit
import Then

class HLTabBarViewController: UITabBarController {

     private let playView = HLTabBarPlayView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.delegate = self
        
        // TabBar 设置
        UITabBar.appearance().isTranslucent = false
        UITabBar.appearance().barTintColor = kThemeWhiteColor
        UITabBar.appearance().tintColor = kThemeTomatoColor
        UITabBar.appearance().backgroundImage = UIImage.init()
        UITabBar.appearance().shadowImage = UIImage.init()
        
        // 设置tabBarItem选中与未选中的文字颜色
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : kThemeWhiteColor], for: UIControlState.normal)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : kThemeTomatoColor], for: UIControlState.selected)
        
        self.tabBar.layer.shadowColor = kThemeGreyColor.cgColor
        self.tabBar.layer.shadowOpacity = 0.8
        self.tabBar.layer.shadowOffset = CGSize(width: 0, height: -1)
        self.tabBar.layer.shadowRadius = 3

        // 初始化子控制器
        self.initSubViewControllers()
        
        // 初始化播放按钮
        self.initPalyView()
        
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        view.bringSubview(toFront: playView)
    }
    
}
// MARK: - 初始化
extension HLTabBarViewController  {
    
    // MARK:- 是否显示播放按钮
    func isHiddenPlayView(_ isHidden: Bool) {
        
        playView.isHidden = isHidden
    }
    
    // MARK:- 初始化播放按钮
    private func initPalyView() {
        
        view.addSubview(playView)
        view.bringSubview(toFront: playView)
        
        playView.didClickedBtn = { [weak self] (isPlay) in
            guard let `self` = self else { return }
            self.jump2Paly()
        }
        
        playView.snp.makeConstraints { (make) in
            make.width.height.equalTo(HLTabBarPlayView.with())
            make.centerX.equalToSuperview()
            if kis_iPhoneX {
                make.bottom.equalToSuperview().offset(-MetricGlobal.padding / 2 - kiPhoneXBottomH)
            } else {
                make.bottom.equalToSuperview().offset(-MetricGlobal.padding / 2)
            }
        }
    }

    private func initSubViewControllers() {
        
        let classNameArr = ["Home", "Hear", "Find", "Mine"]
        let moduleNameArr = ["首页", "我听", "发现", "我的"]
        
        var tabBarrArray: [UIViewController] = []
        let projectName = self.getProjectName()
        
        for i in 0..<classNameArr.count {
            let clsName = classNameArr[i];
            let lowStr = clsName.lowercased();
            
            let clsType = NSClassFromString(projectName+"HL"+clsName+"ViewController") as! UIViewController.Type
            let vc = clsType.init()
            vc.title = moduleNameArr[i]
    
            let item: UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "tabbar_icon_"+lowStr+"_normal"), selectedImage: UIImage(named: "tabbar_icon_"+lowStr+"_pressed"))
            item.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: -8, right: 0)
            vc.tabBarItem = item
            let navVc = HLNavigationViewController(rootViewController: vc)
            tabBarrArray.append(navVc)

        }
        
        // 插入播放VC
        let placeVC = UIViewController()
        placeVC.view.backgroundColor = kThemeWhiteColor
        tabBarrArray.insert(placeVC, at: 2)
        
        self.viewControllers = tabBarrArray

    }
    
    // 获取项目名称
    private func getProjectName() -> String {
        guard let infoDict = Bundle.main.infoDictionary else {
            return "."
        }
        let key = kCFBundleExecutableKey as String
        guard let value = infoDict[key] as? String else {
            return "."
        }
        return value + "."
    }
    
}

// MARK:- 代理
extension HLTabBarViewController: UITabBarControllerDelegate {
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        
        if viewController == tabBarController.viewControllers![2] {
            return false
        }
        return true
    }
    
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    }
}


// MARK:- 控制器跳转
extension HLTabBarViewController {
    
    // MARK:- 登录
    func jump2Login() {
        let VC = HLNavigationViewController(rootViewController: HLLoginViewController())
        self.present(VC, animated: true, completion: nil)
    }
    
    // MARK:- 播放
    func jump2Paly() {
        
        let VC = HLNavigationViewController(rootViewController: HLPlayViewController())
        self.present(VC, animated: true, completion: nil)
    }
}
